package com.apes.fn.server.workOrder.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.fn.server.servicePlan.model.ServicePlanItem;
import com.apes.fn.server.servicePlan.service.ServicePlanceService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.Arith;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.MapUtil;
import com.apes.scm.account.account.model.Cooperation;
import com.apes.scm.contract.model.Contract;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Channel;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import com.apes.scm.masterdata.product.model.ServeType;
import com.apes.scm.masterdata.stock.model.Location;
import com.apestech.framework.json.JSONObject;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 工单工时
 * Creator: zouyc
 * CreateDate: 2020-04-14 18:50
 * Description:
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = MaintenanceOrder.CONST_TABLE_NAME)
public class MaintenanceOrder extends Model{
    public static final String CONST_TABLE_NAME = "ser_maintenance_order";

    public static final String CONST_TABLE_STATE_CREATE = "create";
    public static final String CONST_TABLE_STATE_INNER = "inner";
    public static final String CONST_TABLE_STATE_OUTER = "outer";
    public static final String CONST_TABLE_STATE_TERMINATION = "termination";
    public static final String CONST_TABLE_STATE_FINISH = "finish";

    public static final String WORK_ORDER_COMMON = "common";
    public static final String WORK_ORDER_INNER = "inner";
    public static final String WORK_ORDER_OUT = "out";

    public static final String WORK_ORDER_WORKSTATE_WAIT = "wait";
    public static final String WORK_ORDER_WORKSTATE_PAUSE = "pause";
    public static final String WORK_ORDER_WORKSTATE_WORKING = "working";
    public static final String WORK_ORDER_WORKSTATE_FINISH = "finish";

    public static final String WORK_ORDER_DISPACH_WAIT = "waitDispatch";
    public static final String WORK_ORDER_DISPACH_DISPATCH = "dispatch";



    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "服务工单", padding = 12)
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "belong_dept_id", referencedColumnName = "id", nullable = false)
    @Comment("所属门店")
    private Dept belongDept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_customer_expand_id", referencedColumnName = "id")
    @Comment("赋能客户车辆扩展信息")
    private FnCustomerExpand fnCustomerExpand;

    @Column(name = "business_type", length = 32)
    @States(name = "单据类型", values = {
            @State(id = "common", name = "普通工单"),
            @State(id = "inner", name = "内协工单"),
            @State(id = "out", name = "外协工单")
    })
    private String businessType = "common";

    @Column(name = "business_type_name", length = 64)
    @Comment("单据类型名称")
    private String businessTypeName = "服务工单";

    @Column(name = "source_type", length = 64)
    @Comment("来源类型")
    private String sourceType ;

    @Column(name = "source_from", length = 64)
    @Comment("来源单号")
    private String sourceFrom ;


    @States(name = "工单明细状态", values = {
            @State(id = "create", name = "新增"),
            @State(id = "inner", name = "内协中"),
            @State(id = "outer", name = "外协中"),
            @State(id = "termination", name = "终止"),
            @State(id = "back", name = "退回(外协重新派工)"),
            @State(id = "finish", name = "完成")
    })
    @Column(name = "state", length = 32, nullable = false)
    private String state = "create";

//    public void setState(String state){
//        if( this.finishDate != null && !(Objects.equals(state,"finish") || Objects.equals(state,"termination") )) throw new RuntimeException("工单已经为完成状态，不能改为其他状态");//当已经是完成状态，像更改为其他状态时候报错
//        this.state = state;
//    }

    @States(name = "工单明细状态", values = {
            @State(id = "wait", name = "待施工"),
            @State(id = "pause", name = "暂停中"),
            @State(id = "working", name = "施工中"),
            @State(id = "finish", name = "完成"),
    })
    @Column(name = "work_state", length = 32, nullable = false)
    private String workState = "wait";

    @States(name = "派工状态", values = {
            @State(id = "none", name = "新增"),
            @State(id = "waitDispatch", name = "待派工"),
            @State(id = "dispatch", name = "已派工"),
            @State(id = "back", name = "退回")
    })
    @Column(name = "dispatche_state", length = 32, nullable = false)
    private String dispatcheState = "none";

    @Column(name = "lock_state", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("锁住状态")
    private boolean lockState = false;
    public void setLockState(boolean lockState){
        if (!lockState) this.rankDate = new Date();
        this.lockState=lockState;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_plan_item", referencedColumnName = "ID")
    @Comment("方案明细编码")
    private ServicePlanItem servicePlanItem;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品")
    private Product product;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位编码")
    private ProductUom uom;

    @Column(name = "sale_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("销售数量")
    private double saleQty = 0;

    @Column(name = "product_rule", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("带安标记")
    private boolean productRule;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inner_dept_id", referencedColumnName = "id")
    @Comment("内协门店")
    private Dept innerDept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "termination_uid", referencedColumnName = "id")
    @Comment("终止人")
    private PartyRole terminationUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "termination_date")
    @Comment("终止日期")
    private Date terminationDate;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "rank_date")
    @Comment("排位日期")
    private Date rankDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "finish_uid", referencedColumnName = "id")
    @Comment("完成人")
    private PartyRole finishUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "finish_date")
    @Comment("完成日期")
    private Date finishDate;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "work_start_time")
    @Comment("施工开始时间")
    private Date workStartTime;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "to_inner_uid", referencedColumnName = "id")
    @Comment("转内协人")
    private PartyRole toInnerUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "to_inner_date")
    @Comment("转内协日期")
    private Date toInnerDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dispatch_uid", referencedColumnName = "id")
    @Comment("派工人")
    private PartyRole dispatchUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "dispatch_date")
    @Comment("派工日期")
    private Date dispatchDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dispatch_back_uid", referencedColumnName = "id")
    @Comment("派工退回人")
    private PartyRole dispatchBackUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "out_back_date")
    @Comment("外协退回日期")
    private Date outBackDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "out_back_uid", referencedColumnName = "id")
    @Comment("外协退回人")
    private PartyRole outBackUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "dispatch_back_date")
    @Comment("派工退回日期")
    private Date dispatchBackDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "to_out_uid", referencedColumnName = "id")
    @Comment("转外协人")
    private PartyRole toOutUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "to_out_date")
    @Comment("转外协日期")
    private Date toOutDate;

    @Comment("外协类型")
    @States(name = "外协类型", values = {
            @State(id = "notOut", name = "非外协"),
            @State(id = "inPlatform", name = "平台内"),
            @State(id = "outPlatform", name = "平台外")
    })
    @Column(name = "out_assist_type", nullable = false, columnDefinition = "varchar2(16) default 'notOut'")
    private String outAssistType;

    @States(name = "确定外协类型", values = {
            @State(id = "false", name = "平台外"),
            @State(id = "true", name = "平台内")
    })
    @Column(name = "out_assist_type_confirm", nullable = false, columnDefinition = "number(1,0) default 1")
    private boolean outAssistTypeConfirm = true;

    @Column(name ="out_assist_remark", columnDefinition = "CLOB")
    @Comment("平台外协助原因")
    private String outAssistRemark;

    @Comment("平台外协助原因(枚举)" ) // 新
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inquiry_reason_type", referencedColumnName = "id" )
    private Enumeration inquiryReasonType ;

    @Column(name = "platform_price_cost_in_tax", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("平台成本单价")
    private double platformPriceCosInTax = 0;   // 供应商对平台的价格

    @Column(name = "price_cost_unit_in_tax", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("含税成本单价")
    private double priceCostUnitInTax = 0;      // 平台对赋能店的的价格

    /**
     *
     * @author czs
     * @creed: 目前只有卡内在用
     * @date  2023年6月27日 11:08:30
     */
    @Column(name = "platform_purchase_cost", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("平台采购成本单价")
    private double platformPurchaseCost = 0;

    /**
     * 转外协时填写 生成外协工单的时候 成为外协工单的 平台成本单价(platformPriceCosInTax)
     */
    @Column(name = "platform_tax_temp", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("平台成本单价(外协)")
    private double platformPriceCosInTaxTemp = 0;

    @Column(name = "platform_ex", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("平台不含税成本单价(外协)")
    private double platformPriceCosExTax = 0;

    @Column(name = "price_tax_temp", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("含税成本单价(外协)")
    private double priceCostUnitInTaxTemp = 0;

    @Column(name = "materials_cost", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("材料成本")
    private double materialsCost = 0;

    @Column(name = "work_fee", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("施工费")
    private double workFee = 0;

    @Column(name = "site_fee", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("场地费")
    private double siteFee = 0;

    @Column(name = "plat_fee", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("平台费")
    private double platFee = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contract_id", referencedColumnName = "id")
    @Comment("采购合同")
    private Contract contract;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "reach_date")
    @Comment("供应商到店日期")
    private Date reachDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "platform_id", referencedColumnName = "id")
    @Comment("平台")
    private PartyRole platform;

    @Column(name ="worker1", columnDefinition = "CLOB")
    @Comment("工序施工人1")
    private String worker1;

    @Column(name ="worker2", columnDefinition = "CLOB")
    @Comment("工序施工人2")
    private String worker2;

    @Column(name ="worker3", columnDefinition = "CLOB")
    @Comment("工序施工人3")
    private String worker3;

    @Column(name ="worker4", columnDefinition = "CLOB")
    @Comment("工序施工人4")
    private String worker4;

    @Column(name ="worker1_show", columnDefinition = "CLOB")
    @Comment("工序施工人1")
    private String worker1Show;

    @Column(name ="worker2_show", columnDefinition = "CLOB")
    @Comment("工序施工人2")
    private String worker2Show;

    @Column(name ="worker3_show", columnDefinition = "CLOB")
    @Comment("工序施工人3")
    private String worker3Show;

    @Column(name ="worker4_show", columnDefinition = "CLOB")
    @Comment("工序施工人4")
    private String worker4Show;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;  //单据所属公司

    @Column(name = "work_platform", length = 32)
    @States(name = "施工端", values = {
            @State(id = "cosmetology", name = "美容"),
            @State(id = "machineRepa", name = "机修"),
            @State(id = "sprayPaint", name = "钣喷"),
            @State(id = "other", name = "不施工")
    })
    private String workPlatform;

    public String obtainWorkPlatForm(){
        String re = "其他";
        if("cosmetology".equals(this.workPlatform)){
            re = "美容";
        }else if("machineRepa".equals(this.workPlatform)){
            re = "机修";
        }else if("sprayPaint".equals(this.workPlatform)){
            re = "钣喷";
        }
        return re;
    }


    @Column(name = "dept_remark", length = 4000)
    @Comment("门店备注")
    private String deptRemark;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "settlement_type", referencedColumnName = "id")
    @Comment("结算方式（month：月结，halfMonth：半月结，week：周结，bill：账单，totalCash：总部现金，deptCash：门店现金）")
    private Enumeration settlementType;

    @Column(name = "worked_time", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("已工作时长")
    private double workedTime = 0;

    @Column(name = "pauseed_time", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("已暂停时长")
    private double pauseedTime = 0;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "point_start_time")
    @Comment("节点开始时间")
    private Date pointStartTime;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "work_type", referencedColumnName = "id")
    @Comment("施工方式编码")
    private Enumeration workType; //施工方式

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "work_space", referencedColumnName = "id")
    @Comment("集群空间")
    private Dept workSpace;

    @Column(name ="point", length = 32)
    @Comment("当前节点")
    @States(name = "施工端", values = {
            @State(id = "start", name = "开始类"),
            @State(id = "pause", name = "暂停类"),
            @State(id = "end", name = "完结类")
    })
    private String point;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "receipted_time")
    @Comment("快递签收时间")
    private Date receiptedTime;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "factory_fittings_time")
    @Comment("工厂配货时间")
    private Date factoryFittingsTime;

    /*@Column(name = "broker_product_pick", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("代理拿货价")
    private double brokerProductPick = 0;*/

    @Column(name = "commission_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("佣金额")
    private double commissionAmount = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sale_channel", referencedColumnName = "id")
    @Comment("销售渠道")
    private Channel saleChannel;


    @Column(name = "contain_space",columnDefinition = "number(1,0) default 0")
    @Comment("空间属性")
    private boolean containSpace=false;

    @Transient
    @Comment("销售单价")
    private Double pftPriceUnit;
    public Double getPftPriceUnit(){
        if(this.servicePlanItem != null) return this.servicePlanItem.getPftPriceUnit();
        return null;
    }

    @Column(name = "supplier_ref", length = 20)
    @Comment("第三方单号")
    private String supplierRef;


    @Transient
    private Location location;  //程序需要，中间递归外协设置带安库

    @Transient
    public String assistType;
    public String getAssistType(){
        return this.getServicePlanItem().getAssistType().getCode();
    }

    @Transient
    private PartyRole supplier;
    public PartyRole getSupplier(){
        if(this.getContract()!=null)  return this.getContract().getPartner();
        return null;
    }

    @Transient
    private Cooperation cooperation;
    public Cooperation getCooperation(){
        if(this.contract != null){
            return this.contract.getCooperation();
        }else{
            return null;
        }
    }

    @Transient
    private String wantInvoice;
    public String getWantInvoice(){
        if(this.belongDept != null){
            return this.belongDept.getCompany().getParty().getCorporate().isWantInvoice() ? "开票" : "不开票";
        }else{
            return null;
        }
    }
    @Transient
    public boolean receivablesFlag;
    public boolean getReceivablesFlag(){
        if(this.servicePlanItem.getServicePlan() == null || this.servicePlanItem.getServicePlan().getReceivableDate() == null){
            return false;
        }else{
            return true;
        }
    }

    @Transient
    private PartyRole _creator;
    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

//    @OneToMany(mappedBy = "maintenanceOrder", cascade = CascadeType.ALL, orphanRemoval = true)
//    private List<ConstructionRecord> constructionRecords = new ArrayList<>(); //施工记录
//
//    public List<ConstructionRecord> getConstructionRecords() {
//        for (ConstructionRecord constructionRecord : constructionRecords){
//            constructionRecord.setMaintenanceOrder(this);
//        }
//        return constructionRecords;
//    }
    @Transient
    private Map fnCustomerExpandToForien;  //系统外的操作员
    public Map getFnCustomerExpandToForien(){
        if(this.fnCustomerExpand == null){
            return null;
        }else{
            return MapUtil.mapper(
                "id",this.fnCustomerExpand.getId(),
                        "mobile",this.fnCustomerExpand.getMobile(),
                        "carLicense",this.fnCustomerExpand.getCarLicense()
                    );
        }
    }

    @Column(name = "this_taxRate", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("当前工单税率")
    private Double taxRate;
    @Transient
    private String servicePlanId;
    public String getServicePlanId(){
        if(this.servicePlanItem != null && this.getServicePlanItem().getServicePlan() != null) return  this.getServicePlanItem().getServicePlan().getId();
        return null;
    }

    @Transient
    private PartyRole operator1;  //系统外的操作员

    //用来作临时操作选中状态
    @Transient
    private boolean selected;
    //用来临时操作绑定的配件
    @Transient
    private List<ServicePlanItem> parts;
    //临时存储施工人
    @Transient
    private List<PartyRole> worker;

    @Transient
    public ServeType serveType;
    public ServeType getServeType(){
        if(this.getServicePlanItem() == null) return null;
        return this.getServicePlanItem().getServeType();
    }

    @Transient
    private String saleQtyServiceType;
    public String getSaleQtyServiceType(){
        return (int)this.saleQty + this.getServicePlanItem().getUom().getName();
    }
    /**
     * 是否施工的明细行（包括已经施工完成的）
     * [1.非外协 2.非转过内些的明细行]
     * @return
     */
    public boolean checkWork(){
        boolean flag = true;
        if(this.toInnerDate != null) flag = false;
        if(this.toOutDate != null) flag  = false;
        if(this.terminationDate != null) flag  = false;
        return flag;
    }

    public List returnWorker(){
        List list = new ArrayList();
        if(this.worker1 != null) list.addAll(transferWorker(this.worker1));
        if(this.worker2 != null) list.addAll(transferWorker(this.worker2));
        if(this.worker3 != null) list.addAll(transferWorker(this.worker3));
        if(this.worker4 != null) list.addAll(transferWorker(this.worker4));
        return list.size() == 0 ? null : list;
    }

    private List transferWorker(String worker){
        List list = JSONArray.parseArray(worker,HashMap.class);
        return list;
    }

    /**
     * 检测是否能转内协
     * @return
     */
    public boolean checkCanInnerOrOut(){
        boolean flag = true;
        if(!MaintenanceOrder.CONST_TABLE_STATE_CREATE .equals(this.state)) flag = false;
        return flag;
    }


    public String changeWorkerToWorkerShow(String worker){
        if(worker == null) return null;
        List<HashMap> list = transferWorker(worker);
        String result = "";
        List<String> idList = new ArrayList<>();
        if(list.size() != 0){
            for(int i = 0;i<list.size();i++){
                HashMap map = list.get(i);
                String sName = (String)map.get("name");
                String id = (String) map.get("id");
                if (idList.contains(id)) throw new RuntimeException("同一工序施工人员不允许重复!!!");
                idList.add(id);
                if(sName != null && sName.trim() != null){
                    result = result.trim().equals("") ? sName :  result + "," + sName;
                }
            }
        }
        return result;
    }

    /**
     * 设置外协类型：设置确定值
     */
    public void  transferOutAssistConfirm(){
        if("inPlatform".equals(this.outAssistType)){
            this.setOutAssistTypeConfirm(true);
        }else if("outPlatform".equals(this.outAssistType)){
            this.setOutAssistTypeConfirm(false);
        }else{
            throw new RuntimeException("外协类型有误");
        }
    }

    /**
     * 设置外协类型：设置非确定值字段
     */
    public void  transferOutAssist(){
        if(this.outAssistTypeConfirm){
            this.setOutAssistType("inPlatform");
        }else{
            this.setOutAssistType("outPlatform");
        }
    }

    public Map getFakerSupplier() {
        Contract contract = this.getContract();
        if (contract ==null||contract.getPartner()==null)return null;
        switch (contract.getPartner().getId()){
            case "A00000":
            case "004465":
            case "G1007":
                return MapUtil.mapped(contract.getPartner());
            default:
                return MapUtil.mapped(SpringManager.getBean(PartyRoleRepository.class).findOne("G1007"));

        }

    }

    @Transient
    private Map fakerSupplier;

    public String getStateName() {
        switch (this.state){
            case "create":
                return "创建";
            case "inner":
                return "内协";
            case "outer":
                return "外协中";
            case "termination":
                return "终止";
            case "back":
                return "退回";
            case "finish":
                return "完成";
                default: return stateName;
        }
    }

    @Transient
    private String stateName;

    public String getWorkStateName() {
        switch (this.workState){
            case "wait":
                return "待施工";
            case "pause":
                return "暂停中";
            case "working":
                return "施工中";
            case "finish":
                return "完成";
                default: return workStateName;
        }
    }

    @Transient
    private String workStateName;
}
